#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include "CouplesList.h"
#include "PersonList.h"
#include "Person.h"

using namespace std;
/** \brief This class executes the dating service, matching couples by amount of similar interests.
*
*/
class Dating
{
private:
	PersonList<Person> girls, boys;
	CouplesList<Couple> couples;
	int preferredMatchRate = 0;
public:

	Dating()
	{
	}
	// Format (name, interest1, interest2, interest3...)
	void ReadGirlsFromFile(string filename)
	{
		string line;
		fstream ifs;
		ifs.open(filename);
		
		while (getline(ifs, line))
		{
			istringstream iss(line);
			string s;
			getline(iss, s, '(');
			getline(iss, s, ',');
			Person p(s);
			while (getline(iss, s, ','))
			{
				if (s[0] != ')')
					p.AddInterest(s);
			}
			girls.add_at_head(p);
		}
	}

	void ReadBoysFromFile(string filename)
	{
		string line;
		fstream ifs;
		ifs.open(filename);

		while (getline(ifs, line))
		{
			istringstream iss(line);
			string s;
			getline(iss, s, '(');
			getline(iss, s, ',');
			Person p(s);
			while (getline(iss, s, ','))
			{
				if (s[0] != ')')
					p.AddInterest(s);
			}
			boys.add_at_head(p);
		}
	}

	void PreferredMatchRate()
	{
		cout << "How many matching interests?" << endl;
		cin >> preferredMatchRate;
	}

	void ExecuteCheck()
	{
		CheckForCouple(girls, boys, couples);
	}

	void CheckForCouple(PersonList<Person> girls, PersonList<Person> boys, CouplesList<Couple> &couples)
	{
		int noOfMatchingInterests = 0;
		bool match = true;

		while (match)
		{
			match = false;

			for (auto &girl : girls)
			{
				for (auto &boy : boys)
				{
					for (auto &gi : girl.info.GetInterests())
					{
						if (boy.info.CheckIfMatch(gi.info))
							noOfMatchingInterests++;

						if (noOfMatchingInterests >= preferredMatchRate)
						{
							couples.add_at_head(Couple(girl.info, boy.info));
							girls.remove(&girl);
							boys.remove(&boy);
							match = true;
							noOfMatchingInterests = 0;
							break;
						}
					}
					noOfMatchingInterests = 0;
					if (match)
						break;
				}
				if (match)
					break;
			}
		}
	}


	void PrintCouples()
	{
		for (auto &couple : couples)
		{
			cout << couple.info.GetGirl().GetName() << " & " << couple.info.GetBoy().GetName() <<  endl;
		}
	}

	~Dating()
	{
	}
};

